<?php
include("header.php");

print "<center><h1>New User Registration</h1></center>\n";
print "<center>\n";

$cmd = $_REQUEST["submit"];
$username = $_REQUEST["username"];
$first_name = $_REQUEST["first_name"];
$email = $_REQUEST["email"];
$password = $_REQUEST["password"];
$password_check = $_REQUEST["password_check"];

if($cmd=="Create User"){	
	CreateNewUser();
}else{
	PrintForm();
}

print "</center>\n";
include("footer.php");

//---------------------------
// PrintForm
//---------------------------
function PrintForm()
{
	global $username, $first_name, $email, $password, $password_check;

	?>
	
	<table cellpadding="20" fillcolor="black" border="1"><tr><td>
	
	<form method="POST">
	<table border="0" cellpadding="10">
		<tr>
			<td><input type="entry" size="12" name="username" value="<?=$username?>"></td>
			<td>username *</td>
		</tr>
		<tr>
			<td><input type="entry" size="12" name="first_name" value="<?=$first_name?>"></td>
			<td>First Name *</td>
		</tr>
		<tr>
			<td><input type="entry" size="12" name="email" value="<?=$email?>"></td>
			<td>E-mail (optional)</td>
		</tr>
		<tr>
			<td><input type="password" size="12" name="password" value="<?=$password?>"></td>
			<td>Password *</td>
		</tr>
		<tr>
			<td><input type="password" size="12" name="password_check" value="<?=$password_check?>"></td>
			<td>Re-type password *</td>
		</tr>
		<tr>
			<td colspan="2"><center><input type="submit" name="submit" value="Create User"></center></td>
		</tr>
	</table>
	</form>
	
	</td></tr></table>
	
	<?php
}

//---------------------------
// CreateNewUser
//---------------------------
function CreateNewUser()
{
	global $username, $first_name, $email, $password, $password_check;

	// Check if a user with this username exists
	if(DoesUserExist($username)){
		print "<p><font size=\"+1\" color=\"red\">ERROR: username \"$username\" already exists!</font></p>\n";
		PrintForm();
	}else if(strlen($email)>0 && !filter_var($email, FILTER_VALIDATE_EMAIL)){
		print "<p><font size=\"+1\" color=\"red\">ERROR: e-mail address is invalid! (leave blank if no email address)</font></p>\n";
		PrintForm();
	}else if(strlen($password)<4){
		print "<p><font size=\"+1\" color=\"red\">ERROR: Password should be at least 4 characters!</font></p>\n";
		PrintForm();
	}else if($password!=$password_check){
		print "<p><font size=\"+1\" color=\"red\">ERROR: Passwords don't match!</font></p>\n";
		PrintForm();
	}else{
		CreateUser($username, $first_name, $email, $password);
		
		?>
		<center>
		<META HTTP-EQUIV="refresh" CONTENT="3; URL=index.php">
		<p><font size="+1">Account created for <?=$first_name?> (username=<?=$username?>)!</font></p>
		</center>
		<?php
		
	}
}

?>